<?php	
	require_once KLEO_LIB_DIR.'core.lib.php';

	if(KLEO_ACTUAL_MODULE !== 'comments'){
		require_once KLEO_MODULES_DIR.'comments/models/data.php';
	}
	class comments extends commentsmodel{
	
		/* Call to the model Class */
		function __construct(){
			$this->index();
		}

		function redir(){
			$this->appredirect("index.php?module=blog");
		}

		function generate_comments_box($section, &$cobuff){
			if(!isset($_SESSION['uid'])){
				$this->tpl_load('form.tpl', $cbuff, false, 'comments');
			} else {
				$this->tpl_load('form_logged.tpl', $cbuff, false, 'comments');
			}
			$this->compile($cbuff, $cbuff);
			$this->tpl_set("ICON_THEME", KLEO_CURRENT_ICONS, $cbuff, $cbuff);
			$cobuff = $cbuff;
		}

		function get_number_comments($itemid, $type){

			$res = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments WHERE costatus = 1 AND copostid = $itemid AND cosection = $type");
			$num = $this->num_rows($res);
			if($num == 0){
				return L_NO_COMMENTS_YET;
			} else {
				return $num;
			}

		}

		function add_comment($postid, $section, $name=false, $email=false, $web=false, $comment=false, $aid=false){
			$comment = $this->escape_query_string($comment);
			$name = $this->escape_query_string($name);

			if(empty($aid)){
				$aid=$_SESSION['aid'];
			}

			if(!is_numeric($postid) && $aid==0 or !is_numeric($section) && $aid==0){
				$this->apperror("Ilegal operation ¬¬", "common");
			}

			if(!$this->is_web($web) && $web !== 'http://' && !empty($web) && $aid==0){
				$this->apperror(L_WEB_NOT_VALID, "common");
			}

			if(!$this->is_email($email) && $aid==0){
				$this->apperror(L_EMAIL_NOT_VALID, "common");
			}

			if(empty($name) && $aid==0 or empty($email) && $aid==0 or empty($comment) && $aid==0){
				$this->apperror(L_EMPTY_FIELDS, "common");
			}

			$date = $this->get_date();
			$ip = $_SERVER['REMOTE_ADDR'];
			$status = $this->get_core_value("comments_moderate");
			$comment = $comment;

			$this->query("INSERT INTO ".KLEO_TABLE_PREFIX."comments (cotitle, cotext, coweb, coemail, codate, coaid, cosection, copostid, costatus, coip) VALUES ('$name', '$comment', '$web', '$email', '$date', '$aid', '$section','$postid', $status,  '$ip');");

			if($aid == 0){
				$from = $name;
			} else {
				$from = $this->get_username($aid);
			}

			$link = $this->get_core_value('siteurl').'/?module=blog&p='.$postid.'&comment-'.$this->get_last_comment_id();
			if($this->get_core_value("comments_email_notification") == 1){
				$this->sendmail($this->get_core_value('admin_email'), '['.$this->get_core_value('web_name').'] '.L_NEW_COMMENT_FROM.$from,L_NEW_COMMENT_MESSAGE.$link);
			}
			$this->send_notifications($postid, $email);
		}

		function send_notifications($postid,$avoid){
				$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments, ".KLEO_TABLE_PREFIX."blog WHERE copostid = id and id = $postid");
				while($row = $this->fetch_array($query)){
					if(!$emails[$row['coemail']] && $row['coemail'] !== ''){
						$email = $row['coemail'];
						$emails[$row['coemail']] = true;
					} else if($row['coemail'] == ''){
						$user = $this->get_user_info($row['coaid']);
						$email = $user['email'];
					} else {
						unset($email);
					}
					$link = $this->get_core_value('siteurl').'/?module=blog&p='.$postid.'&comment-'.$this->get_last_comment_id();
					if($email && !$email !== $avoid){
						$this->sendmail($email, '['.$this->get_core_value('web_name').'] '.L_NEW_COMMENT_AT.' '.$this->get_core_value('web_name'),L_NEW_COMMENT_NOTIFICATION.$link);
					}
				}
		}

		function get_comments($postid, $section, &$cobuff){

			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments WHERE costatus = 1 AND cosection = '$section' AND copostid = '$postid'");
			$c = "";
			$s=1;
			$i=1;
			while($row = $this->fetch_array($query)){
				if($row['coaid']>0){
					$author = $this->get_user_info($row['coaid']);
					if(!empty($author['nicename'])){
						$row['cotitle'] = $author['nicename'];
					} else {
						$row['cotitle'] = $author['username'];
					}
					$row['coemail'] = $author['email'];
					$row['coweb'] = '?module=users&action=uid&uid='.$author['uid'];				}
				if($s==1){ 
					$s=2;
				} else {
					$s=1;
				}
				if(empty($row['coweb'])){
					$row['coweb'] = '#comment-'.$row['coid'];
				}
				if(file_exists(KLEO_FULL_TEMPLATE_ROUTE.'comments/show.tpl')){
					$this->tpl_load(KLEO_FULL_TEMPLATE_ROUTE.'comments/show.tpl', $cbuff);
				} else {
					$this->tpl_load(KLEO_MODULES_DIR.'comments/views/show.tpl', $cbuff);
				}
				$this->tpl_set(array(
					"CO_NAME" => $row['cotitle'],
					"CO_ID" => $row['coid'],
					"CO_EMAIL" => $row['coemail'],
					"CO_WEB" => $row['coweb'],
					"CO_DATE" => $row['codate'],
					"CO_TEXT" => utf8_decode($row['cotext']),
					"CO_COUNT" => $i,
					"SWITCH" => $s
				),false, $cbuff, $cbuff);
				$this->compile($cbuff, $cbuff);
				$c .= $cbuff;
				$i++;
			}
			$cobuff = $c;

		}

		function get_last_comment_id(){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."comments ORDER BY coid DESC LIMIT 1");
			$row = $this->fetch_array($query);
			return $row['coid'];
		}

	}
?>
